<?php

declare(strict_types=1);

namespace App\Admin\Controller\System\DataCenter;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\System\Notice\SystemNoticeCreateRequest;
use App\Admin\Request\System\Notice\SystemNoticeUpdateRequest;
use App\Admin\Service\System\SystemNoticeService;
use DI\Attribute\Inject;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use support\Request;
use support\Response;

/**
 * 通知管理控制器
 * Class NoticeController
 */
#[Controller(prefix: "system/notice"), Auth]
class NoticeController extends NewbieController {
    #[Inject]
    protected SystemNoticeService $service;

    /**
     * 列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("system:notice:index")]
    public function index (Request $request): Response {
        return $this->success($this->service->getPageList($request->all()));
    }

    /**
     * 回收站列表
     * @param Request $request
     * @return Response
     */
    #[GetMapping("recycle"), Permission("system:notice:recycle")]
    public function recycle (Request $request): Response {
        return $this->success($this->service->getPageListByRecycle($request->all()));
    }

    /**
     * 新增
     * @param SystemNoticeCreateRequest $request
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Throwable
     */
    #[PostMapping("save"), Permission("system:notice:save")]
    public function save (SystemNoticeCreateRequest $request): Response {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 读取数据
     * @param int $id
     * @return Response
     */
    #[GetMapping("read/{id}"), Permission("system:notice:read")]
    public function read (int $id): Response {
        return $this->success($this->service->read($id));
    }

    /**
     * 更新
     * @param int $id
     * @param SystemNoticeUpdateRequest $request
     * @return Response
     */
    #[PutMapping("update/{id}"), Permission("system:notice:update")]
    public function update (int $id, SystemNoticeUpdateRequest $request): Response {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量删除数据到回收站
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("system:notice:delete")]
    public function delete (string $ids): Response {
        return $this->service->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量真实删除数据 （清空回收站）
     * @param String $ids
     * @return Response
     */
    #[DeleteMapping("realDelete/{ids}"), Permission("system:notice:realDelete")]
    public function realDelete (string $ids): Response {
        return $this->service->realDelete($ids) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量恢复在回收站的数据
     * @param String $ids
     * @return Response
     */
    #[PutMapping("recovery/{ids}"), Permission("system:notice:recovery")]
    public function recovery (string $ids): Response {
        return $this->service->recovery($ids) ? $this->success() : $this->error();
    }
}
